package com.tich.user.service.impl;


import com.tich.constant.RabbitMqConstant;
import com.tich.context.UserContext;
import com.tich.user.domain.entity.UserInfo;
import com.tich.user.mapper.UserInfoMapper;
import com.tich.user.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王志宇
 * @since 2025-05-10
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询用户详细信息
     * @return
     */
    @Override
    public UserInfo queryUserInfoById() {
        //TODO 查redis
        Long userId = UserContext.getCurrentId();
        //查询
        UserInfo  userInfo = query().eq("user_id", userId).one();
        //判断是否是第一次
        if (userInfo == null) {
            //第一次查询，存入数据库
            save(UserInfo.builder()
                    .userId(userId)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build());
        }

        //TODO　存入redis
        return userInfo;
    }


    @Override
    public void deleteById(Long userId) {
        UserInfo userInfo = query().eq("user_id",userId).one();
       // removeById(UserContext.getCurrentId());
        rabbitTemplate.convertAndSend(RabbitMqConstant.USER_DIRECT,
                RabbitMqConstant.USERINFO_DELETE_KEY,
                userInfo);
    }

}
